package com.kushe.site.controller;

import com.kushe.dao.MessageDao;
import com.kushe.dao.UserDao;
import com.kushe.entity.Message;
import com.kushe.entity.User;
import com.kushe.util.KSController;
import com.kushe.util.SessionEnv;
import com.kushe.util.core.DaoFactory;
import org.apache.commons.lang3.StringEscapeUtils;
import org.nutz.dao.QueryResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by WhiteBlue on 15/5/16.
 */

@org.springframework.stereotype.Controller
@RequestMapping(value = "/message")
public class MessageController extends KSController {

    @RequestMapping(value = "", method = RequestMethod.GET)
    public Object checkMessage(@RequestParam(value = "page", required = false, defaultValue = "1") int page, HttpServletRequest request) {
        clearAttr(request);
        User user = ((SessionEnv) request.getSession().getAttribute("sessionEnv")).getUser();
        MessageDao messageDao = DaoFactory.getDao(MessageDao.class);
        QueryResult queryResult = messageDao.listMessage(user.getId(), page);
        this.setAttr("page", queryResult);
        this.setAttr("actionUrl", "/message?page=");
        if (queryResult.getList().size() == 0) {
            this.setAttr("none", true);
        }
        return this.render("/user/myMessage.html");
    }


    @RequestMapping(value = "/check", method = RequestMethod.GET)
    public Object checkMessage(@RequestParam(value = "message", required = true) int messageId, @RequestParam(value = "page", required = false, defaultValue = "1") int page, HttpServletRequest request) {
        clearAttr(request);
        User user = (User) request.getSession().getAttribute("sessionEnv");
        Message message = DaoFactory.getDao(MessageDao.class).get(messageId);
        if (message != null) {
            MessageDao dao = DaoFactory.getDao(MessageDao.class);
            QueryResult queryResult = dao.listReply(messageId, page, user.getId());
            if (queryResult != null) {
                this.setAttr("page", queryResult);
                this.setAttr("message", message);
                this.setAttr("actionUrl", "/check?message=" + messageId + "&page=");
                return this.render("/user/myMessageInfo.html");
            } else {
                return this.renderError("请求id有误");
            }
        } else {
            return this.renderError("请求id有误");
        }
    }


    //寻找用户接口
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    @ResponseBody
    public Object findUser(@RequestParam(value = "name", required = true) String name, HttpServletRequest request) {
        clearAttr(request);
        UserDao userDao = DaoFactory.getDao(UserDao.class);
        Map<String, Object> json = new WeakHashMap<String, Object>();
        List<Map<String, Object>> jsonList = new ArrayList<Map<String, Object>>();
        for (User user : userDao.getByName(StringEscapeUtils.escapeJava(name))) {
            Map<String, Object> temp = new WeakHashMap<String, Object>();
            temp.put("name", user.getName());
            temp.put("id", user.getId());
            jsonList.add(temp);
        }
        json.put("list", jsonList);
        return json;
    }


    //发送私信
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Object addMessage(@RequestParam(value = "toId", required = true) int toId, @RequestParam(value = "content", required = true) String content, HttpServletRequest request) {
        clearAttr(request);
        User user = ((SessionEnv) request.getSession().getAttribute("sessionEnv")).getUser();
        UserDao userDao = DaoFactory.getDao(UserDao.class);
        MessageDao dao = DaoFactory.getDao(MessageDao.class);
        User to_user = userDao.get(toId);
        Message message = null;
        Map<String, Object> json = new WeakHashMap<String, Object>();

        if (to_user != null) {
            if (!dao.haveMessage(user.getId(), toId)) {
                message = dao.createMessage(user.getId(), toId, content, new Date());
            } else {
                message = dao.addReply(user.getId(), toId, content, new Date());
            }
            if (message != null) {
                json.put("result", 1);
                return json;
            } else {
                json.put("result", 0);
                return json;
            }
        } else {
            json.put("result", 2);
            return json;
        }
    }


}
